package com.niit.ai.dl.dltrain.service;

import com.niit.ai.dl.dltrain.dao.model.Model;
import com.niit.ai.dl.dltrain.dao.ModelDao;

import com.niit.ai.dl.dltrain.web.model.ModelListRsp;
import com.niit.ai.dl.base.BaseRsp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ModelService {
    private static final Logger LOG = LoggerFactory.getLogger(ModelService.class);

    @Autowired
    private ModelDao modelDao;

    public ModelListRsp queryList(Integer currentPage, Integer pageSize) {
        int total = modelDao.total();

        int start = (currentPage -1) * pageSize;
        if (start >= total) {
            LOG.info("start {} >= total {}", start, total);
            start = 0;
        }

        List<Model> records = modelDao.queryList(start ,pageSize);

        ModelListRsp ret = new ModelListRsp();
        ret.setTotal(total);
        ret.setRecords(records);
        return ret;
    }

    public BaseRsp add(Model model) {

        int count = modelDao.insert(model);
        LOG.info("insert count {}, model {}", count, model);
        return new BaseRsp();
    }

    public BaseRsp delete(Integer id) {
        int count = modelDao.delete(id);
        LOG.info("delete count {}", count);
        return new BaseRsp();
    }

    public BaseRsp update(Model model) {
        int count = modelDao.update(model);
        LOG.info("update count {}", count);
        return new BaseRsp();
    }

    public ModelListRsp queryListByType(Integer modelTypeId) {
        List<Model> records = modelDao.queryListByType(modelTypeId);

        ModelListRsp ret = new ModelListRsp();
        ret.setRecords(records);
        return ret;
    }
}
